﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Sy;
using Texfina.Entity.Mg;
using RHBOQry = Texfina.BOQry.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using MGBOQry = Texfina.BOQry.Mg;
using Newtonsoft.Json;

namespace RH
{
    public partial class wfPrograVacaBnd : SearchPage
    {
        public const int _GRDLIST_IDESTADO = 8;

        protected void Page_Load(object sender, EventArgs e)
        {

            this.LoadInitialSettings();
            if (this.IsPostBack)
            {
                this.LoadInPostBack();
                return;
            }

            this.LoadData();
            SearchData();

        }

        public override void LoadInitialSettings()
        {
            Label lbl = (Label)this.Master.FindControl("lblTitle");
            lbl.Text = "Programación de Vacaciones";
            this.Page.Form.DefaultButton = bttSearch.UniqueID;
            this.hdnMsgOPB.Value = GetMessage();
        }

        public override void LoadListControls()
        {
            //FOR FILTER
            if (Request.QueryString["Filter"] == null)
                Session[ConstSessionVar.UFILTER] = "";
            else
                hdnFilter.Value = Session[ConstSessionVar.UFILTER].ToString();
            //END FOR FILTER

        }

        public override void SearchData()
        {

            EPersonalBandeja objE = new EPersonalBandeja();


            //FOR FILTER
            DataTable dtToken = (DataTable)JsonConvert.DeserializeObject(hdnFilter.Value, (typeof(DataTable)));
            Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
            textFilter.DataSourceListToken = dtToken;
            objE.Filter = WebHelper.GetFilterWhere(dtToken, "id");
            objE.Filter = WebHelper.AddFilterWhere(objE.Filter, "a.idEmpresa", OperatorWhere.igual, Session[ConstSessionVar.EMPRESA].ToString(), LogicalWhere.AND);
            objE.IdPeriodo = (int.Parse(Session[ConstSessionVar.PERIODO].ToString()) - 1).ToString();
            //FOR FILTER


            RHBOQry.IPersonal objBO = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
            DataTable dt = objBO.GetByCriteriaVacacion(objE);

            this.GridSetDataBinding(grdList, dt);

        }

        protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
        {
            String strQS = "";
            String strIdPlanilla = "";

            switch (e.Item.Value)
            {
                case ConstMenuToolbarValue.BUTTON_NEW:

                    foreach (GridViewRow gvr in this.grdList.Rows)
                    {
                        if (gvr.RowType == DataControlRowType.DataRow)
                        {
                            CheckBox chkAllSelect = (CheckBox)gvr.FindControl("chkSelected");
                            if (chkAllSelect.Checked)
                            {
                                strQS = strQS + gvr.Cells[3].Text + ",";

                                if (strIdPlanilla != "" && strIdPlanilla == gvr.Cells[2].Text)
                                { strIdPlanilla = gvr.Cells[2].Text; }
                                else
                                {
                                    this.hdnMsgOPB.Value = this.CreatePostbackMessage("ATENCIÓN", "El personal seleccionado tiene que pertenecer al misma Planilla", TypeMessageAlert.atention, false, "");
                                    return;
                                }
                            }
                        }
                    }

                    strQS = "wfPrograVacaMasiMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdPersonal=" + strQS;
                    break;
            }
            //FOR FILTER
            Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
            //END FOR FILTER
            Response.Redirect(strQS);
        }

        protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
            FormEditAction formEA = FormEditAction.AddNew;

            switch (e.CommandName)
            {
                case ConstGridRowCommand.GRIDEDIT:
                    formEA = FormEditAction.Edit;
                    break;
                case ConstGridRowCommand.GRIDDELETE:
                    formEA = FormEditAction.Delete;
                    break;
                case ConstGridRowCommand.GRIDPREVIEW:
                    //this.PrintReport(Convert.ToInt32(e.CommandArgument));
                    return;
            }
            //FOR FILTER
            Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
            //END FOR FILTER
            String strQS = "wfPrograVacaMasiMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdPersonal=" + dr["idPersonal"].ToString() + "&IdPlanilla=" + dr["idPlanilla"].ToString();
            Response.Redirect(strQS);
        }

        protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAllChecked = new CheckBox();
                chkAllChecked.ID = "chkAllCheck";
                chkAllChecked.Checked = false;
                chkAllChecked.Attributes.Add("onClick", "SelectedCheckBoxAll(this);");
                e.Row.Cells[0].Controls.Add(chkAllChecked);
            }


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                String strRI = e.Row.RowIndex.ToString();

                if (e.Row.Cells[_GRDLIST_IDESTADO].Text == ConstFlagEstado.DESACTIVADO)
                { e.Row.ForeColor = System.Drawing.Color.Green; }

            }

        }

        protected void bttOk_Click(object sender, EventArgs e)
        {
            this.SearchData();
        }

        //protected void ddlidUNegocio_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ddlidCCosto.Items.Clear();
        //    EUNegocio objEUNeg = new EUNegocio();
        //    MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
        //    objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
        //    objEUNeg.IdUNegocio = ddlidUniNego.SelectedItem.Value;
        //    DataTable dt = objBOUNego.GetCCostoXUNegocio(objEUNeg);
        //    WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", true, true);
        //}
    }
}